package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.TreeviewNode;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.system.entity.Blog;
import cn.stylefeng.guns.modular.system.mapper.BlogMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BlogService extends ServiceImpl<BlogMapper, Blog> {
    @Resource
    private BlogMapper blogMapper;

    /**
     * 新增博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:00 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public void addBlog(Blog blog) {

        if (ToolUtil.isOneEmpty(blog, blog.getAuthor(), blog.getDate(), blog.getLink(), blog.getTitle(),blog.getTag())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }


        this.save(blog);
    }

    /**
     * 修改博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:00 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public void editBlog(Blog blog) {

        if (ToolUtil.isOneEmpty(blog, blog.getAuthor(), blog.getDate(), blog.getLink(), blog.getTitle(),blog.getTag())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        this.updateById(blog);
    }

    /**
     * 删除博客
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    @Transactional
    public void deleteBlog(Long blogId) {
        Blog blog = blogMapper.selectById(blogId);

        //根据like查询删除所有级联的博客
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper = wrapper.like("PIDS", "%[" + blog.getId() + "]%");
        List<Blog> subBlogs = blogMapper.selectList(wrapper);
        for (Blog temp : subBlogs) {
            this.removeById(temp.getId());
        }

        this.removeById(blog.getId());
    }

    /**
     * 获取ztree的节点列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    /**
     * 获取ztree的节点列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public List<TreeviewNode> treeviewNodes() {
        return this.baseMapper.treeviewNodes();
    }

    /**
     * 获取所有博客列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:16 PM
     */
    public Page<Map<String, Object>> list(String condition) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition);
    }


}
